import {request, config} from 'utils'

const {api, defaultPageSize, ROLE_ADMIN_ID} = config;
const {role, menu, securityMeta} = api;

export async function query(params = {}) {
  const limit = params.queryParam.limit
    ? params.queryParam.limit
    : 100;
  const start = params.queryParam.start
    ? params.queryParam.start + 1
    : 1;
  const url = `${role}/list/${limit}/${start}/name/${params.order}/complex`;
  return request({url: url, method: 'PUT', data: params.queryParam})
}

/**
 * 创建角色
 * @param {*} params
 */
export async function create(params) {
  const url = `${role}/create`;
  return request({url: url, method: 'POST', data: params})
}

/**
 * 修改角色
 * @param {*} params
 */
export async function update(params) {
  const url = `${role}/update`;
  return request({url: url, method: 'PUT', data: params})
}

/**
 * 删除角色
 * @param {*} id
 * @param {*} version
 */
export async function deleteRole(id, version) {
  const url = `${role}/delete/${id}/${version}`;
  return request({url: url, method: 'POST', data: {}})
}

/**
 * 获取角色拥有的权限
 * @param {*} roleId
 */
export async function listResourceByRole(roleId) {
  const url = `${role}/listResource/${roleId}`;
  return request({url: url, method: 'GET', data: {}})
}

/**
 * 角色资源绑定
 * @param {*} roleId
 * @param {*} resourceIds
 */
export async function bindResource(roleId, resourceIds) {
  const url = `${role}/bindResource/${roleId}`;
  return request({url: url, method: 'PUT', data: resourceIds})
}

/**
 * 解绑角色资源
 * @param {*} roleId
 * @param {*} resourceIds
 */
export async function unbindResource(roleId, resourceIds) {
  const url = `${role}/unbindResource/${roleId}`;
  return request({url: url, method: 'PUT', data: resourceIds})
}

/**
 * 获取角色已绑用户ID
 * @param {*} roleId
 */
export async function listUserIdByRole(roleId) {
  const url = `${role}/listUserId/${roleId}`;
  return request({url: url, method: 'GET', data: {}})
}

/**
 * 绑定用户
 * @param {*} roleId
 * @param {*} userIds
 */
export async function bindUser(roleId, userIds) {
  const url = `${role}/bindUser/${roleId}`;
  return request({url: url, method: 'PUT', data: userIds})
}

/**
 * 解绑用户
 * @param {*} roleId
 * @param {*} userIds
 */
export async function unbindUser(roleId, userIds) {
  const url = `${role}/unbindUser/${roleId}`;
  return request({url: url, method: 'PUT', data: userIds})
}

export async function bindUserRoles(userId, roleIds) {
  const url = `${role}/bindUserRoles/${userId}`;
  return request({url: url, method: 'PUT', data: roleIds})
}

/**
 * 获取菜单
 */
export async function listMenu() {
  const url = `${menu}/all`;
  return request({url: url, method: 'GET'})
}
/**
 * 根据角色获取菜单
 */
export async function getMenu(roleId) {
  const url = `${menu}/getByRole/${roleId}`;
  return request({url: url, method: 'GET'})
}
/**
 * 绑定菜单
 */
export async function bindMenu(payload) {
  const url = `${menu}/bindByRole/${payload.roleId}`;
  return request({
    url: url,
    method: 'POST',
    data: {
      menuIds: payload.menuIds
    }
  })
}

export async function getUserRoles(userId) {
  const url = `${role}/listUserRole/${userId}`;
  return request({url: url, method: 'GET'})
}

/**
 * 刷新角色资源列表
 */
export async function refreshRequestMap() {
  const url = `${securityMeta}/refreshRequestMap`;
  return request({url: url, method: 'PUT', data: {}})
}

/**
 * 已绑定资源组列表
 */
export async function bindedResourceList(payload) {
  const url = `${role}/getFlagBoundTree/${payload.roleId}`;
  return request({url: url, method: 'GET', data: {}})
}

/**
 * 绑定资源组
 */
export async function bindedResourceGrounp(payload) {
  const url = `${role}/bindResourceGroup/${payload.roleId}`;
  return request({url: url, method: 'PUT', data: payload.params})
}

/**
 * 解绑资源组
 */
export async function unbindedResourceGrounp(payload) {
  const url = `${role}/unbindResourceGroup/${payload.roleId}`;
  return request({url: url, method: 'PUT', data: payload.params})
}

/**
 * 绑定资源组
 * 包括绑定和解绑操作
 * @param {Object} payload
 */
export async function batchUpdateResourceGroup(payload) {
  const url = `${role}/batchUpdateResourceGroup/${payload.roleId}`;
  return request({url: url, method: 'PUT', data: payload.params})
}
